// packageE/ranking_2/index_v2/index_v2.js
import DPagination from "../DPagination";
const IncomePagination = new DPagination("incomeList");
const dayjs = require('../../../utils/dayjs.min')
const App = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    incomeList: {},
    personalRanking: "暂未上榜", //* 个人排名
    platformReward: 0, //* 平台收益
    memberInfo: null,
    imgPath: App.globalData.imgPath,
    typeActive: 1,
    loading: false,
    timeTypeOption: [
      { text: '全部', value: 0 },
      { text: '月度', value: 'month' },
      { text: '年度', value: 'year' }
    ],
    timeTypeValue: 0,
    timePickerVisible: false,
    timeType: null,
    timeValue: null,
    timeText: '',
    minDate: new Date('2024-01-01').getTime()
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    IncomePagination.bind(this);
    IncomePagination.clean();
    this.loadIncomeData();
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    if(this.data.timePickerVisible) {
      // 解决时间选择弹窗滚动导致刷新数据
      wx.stopPullDownRefresh();
      return
    }
    IncomePagination.clean();
    this.loadIncomeData();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if(this.data.timePickerVisible) {
      // 解决时间选择弹窗滚动导致刷获取数据
      return
    }
    this.loadIncomeData();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
  onShareTimeline() {},
  loadIncomeData() {
    if (IncomePagination.loading || IncomePagination.finished) {
      return;
    }
    wx.showLoading({
      title: "加载中",
      mask: true,
    });
    IncomePagination.loading = true;
    let url = App.getNetAddresss(
      "statistics.rank.index"
    );
    let params = {
      rank_type: this.data.typeActive,
      page: IncomePagination.loadPage
    }
    const { timeTypeValue, timeValue, timeType } = this.data
    if (timeTypeValue != 0) {
      // 类型非全部
      params.time_type = timeTypeValue
      if (timeValue) {
        // 如果选择了时间(类型选择，时间未选择，切换选项卡时会出现时间为空的情况)
        // 组件时间戳为13位，后端只接收10位
        params.time = timeValue / 1000
      }
    }
    App._getNetWork({
      url,
      data: params,
      success: ({ data: { result, msg, data: response } }) => {
        if (result == 0) {
          wx.showToast({
            title: msg,
            icon: "none",
          });
          return;
        }
        if (response.data.length < IncomePagination.limit) {
          IncomePagination.finished = true;
        }
        if (IncomePagination.limit != response.per_page) {
          IncomePagination.limit = response.per_page;
        }
        IncomePagination.push(response.data);
        const setData = {};
        if (response.personal_ranking) {
          response.personal_ranking = parseInt(response.personal_ranking);
          if (response.personal_ranking) {
            setData["personalRanking"] =
              "第" + response.personal_ranking + "名";
          }
        }
        if(this.data.memberInfo===null){
          setData['memberInfo']=response.member;
        }
        this.setData(setData);
        IncomePagination.loading = false;
        wx.hideLoading();
        wx.stopPullDownRefresh();
      },
      complete: function() {
        IncomePagination.loading = false
        wx.hideLoading()
      }
    });
  },
  
  /**
   * 选项卡切换
   * @param {Object} e 
   */
  typeChange: function(e) {
    if (IncomePagination.loading) {
      App.tips('数据获取中...')
      return
    }
    const { name } = e.detail
    IncomePagination.clean();
    this.setData({ typeActive: name })
    this.loadIncomeData()
  },

  /**
   * 时间类型改变
   * @param {Object} e 
   */
  timeTypeChange: function(e) {
    if (IncomePagination.loading) {
      App.tips('数据获取中...')
      return
    }
    const { detail } = e
    let timeType = null
    if (detail == 'month') {
      timeType = 'year-month'
    } else if (detail == 'year') {
      timeType = 'year'
    }
    this.setData({
      timeTypeValue: detail,
      timeType,
      timeValue: null,
      timeText: ''
    })
    if (detail == 0) {
      IncomePagination.clean();
      this.loadIncomeData()
    }
  },

  /**
   * 显示时间弹窗
   */
  showTimePicker: function() {
    if(!this.data.timeTypeValue) {
      App.tips('请先选择类型！')
      return
    }
    this.setData({ timePickerVisible: true })
  },

  /**
   * 隐藏时间弹窗
   */
  hideTimePicker() {
    this.setData({ timePickerVisible: false })
  },

  /**
   * 时间确认
   * @param {Object} e 
   */
  timePickerConfirm: function(e) {
    let timeText = ''
    let formatStr = 'YYYY'
    const { timeType } = this.data
    if (timeType == 'year-month') {
      formatStr = 'YYYY-MM'
    }
    // 页面显示日期
    timeText = dayjs(e.detail).format(formatStr)
    this.setData({
      timeValue: e.detail,
      timePickerVisible: false,
      timeText
    })
    // 重新获取数据
    IncomePagination.clean();
    this.loadIncomeData()
  },
});
